<?php
    include("include/header.inc.php");
    include("include/func.inc.php");
    $_SESSION['page'] = 'event';
    
    $previous_page = $_SESSION['previous_page_register'];
    
    //Function to display message when event not found
    function event_not_found($texts){
        echo '<p align="center">'.$texts['event_not_found'][$_SESSION['lang']].'<br/><br/>';
        echo '<a href="./events.php" rel="external" data-role="button" data-inline="true">'.$texts['back'][$_SESSION['lang']].'</a>';
    }
    
    //Function to display content if not event found
    function display_event_not_found($texts){
        echo '<h2>'.$texts['error'][$_SESSION['lang']].'</h2>';
        echo '<div class="tableauPage">';
        event_not_found($texts);
        echo '</p></div>';
    }
    
    //Function to display content if not theme found
    function display_theme_not_found($texts){
        echo '<h2>'.$texts['error'][$_SESSION['lang']].'</h2>';
        echo '<div class="tableauPage">';
        // Only the "event" parameter exists in the url
        if(isset ($_GET['event'])){
            //Error : theme not found
            echo '<p align="center">'.$texts['theme_not_found'][$_SESSION['lang']].'<br/><br/>';
            echo '<a href="./detailEvent.php?event='.$_GET['event'].'" data-role="button" data-inline="true">'.$texts['back'][$_SESSION['lang']].'</a>';
        }
        else {
            //Error : event not found
             event_not_found($texts);
        }
        echo '</p></div>';
    }
    
    function get_detail_theme($rfc, $id_theme){
        //-- FUNCTION MODULE: Z_FM_M646_GET_THEMES --
        //We must know if the function really exists
        $fce_get_themes = saprfc_function_discover($rfc, "Z_FM_M646_GET_THEMES");
        if(!$fce_get_themes){
            echo "The function module has failed.";
            echo $rfc;
            exit;
        }

        //Pass import parameters
        saprfc_import ($fce_get_themes,"IV_THEME",$id_theme);

        //Pass table parameters
        saprfc_table_init ($fce_get_themes,"TAB_DETAILS_THEME");

        //Call and execute the function
        $rc_get_themes = saprfc_call_and_receive ($fce_get_themes);            
        if ($rc_get_themes != SAPRFC_OK){
            if ($rfc == SAPRFC_EXCEPTION )
                echo ("Exception raised: ".saprfc_exception($fce_get_themes));
            else
                echo ("Call error: ".saprfc_error($fce_get_themes));
            exit;
        }

        //Fetch the data from the internal tables
        $data_row = saprfc_table_rows ($fce_get_themes,"TAB_DETAILS_THEME");

        if ($data_row > 0)//If the theme exist
            $DATARow = saprfc_table_read ($fce_get_themes,"TAB_DETAILS_THEME",1);

        //realease the function
        saprfc_function_free($fce_get_themes);

        return $DATARow;
    }
    
    function get_nbParticipants($rfc, $id_prerequis){
        //-- FUNCTION MODULE: Z_FM_M646_GET_PARTICIPANTS --
        //To know the number of suscriptions to this theme to display the future status

        //We must know if the function really exists
        $fce_get_participants = saprfc_function_discover($rfc, "Z_FM_M646_GET_PARTICIPANTS");
        if(!$fce_get_participants){
            echo "The function module has failed.";
            echo $rfc;
            exit;
        }

        //Pass import parameters
        saprfc_import ($fce_get_participants,"IV_THEME",$id_prerequis);

        //Pass table parameters
        saprfc_table_init ($fce_get_participants,"TAB_MEMBERS");

        //Call and execute the function
        $rc_get_participants = saprfc_call_and_receive ($fce_get_participants);            
        if ($rc_get_participants != SAPRFC_OK){
            if ($rfc == SAPRFC_EXCEPTION )
                echo ("Exception raised: ".saprfc_exception($fce_get_participants));
            else
                echo ("Call error: ".saprfc_error($fce_get_participants));
            exit;
        }

        //Fetch the data from the internal tables
        $data =  saprfc_table_rows ($fce_get_participants,"TAB_MEMBERS");

        //realease the function
        saprfc_function_free($fce_get_participants);
        
        return $data;
    }
    
    //Function to get the current status if the registration already exist
    function get_registration($rfc, $id_theme, $username){
        //-- FUNCTION MODULE: Z_FM_M646_GET_REGISTRATION --
        //We must know if the function really exists
        $fce_get_registration = saprfc_function_discover($rfc, "Z_FM_M646_GET_REGISTRATION");
        if(!$fce_get_registration){
            echo "The function module has failed.";
            echo $rfc;
            exit;
        }

        //Pass import parameters
        saprfc_import ($fce_get_registration,"IV_THEME", $id_theme);
        saprfc_import ($fce_get_registration,"IV_MEMBER", $username);

        //Pass table parameters
        saprfc_table_init ($fce_get_registration, "TAB_REGISTRATION");

        //Call and execute the function
        $rc_get_registration = saprfc_call_and_receive ($fce_get_registration);            
        if ($rc_get_registration != SAPRFC_OK){
            if ($rfc == SAPRFC_EXCEPTION )
                echo ("Exception raised: ".saprfc_exception($fce_get_registration));
            else
                echo ("Call error: ".saprfc_error($fce_get_registration));
            exit;
        }

        //Fetch the data from the internal tables
        $data_rowRegistration = saprfc_table_rows ($fce_get_registration,"TAB_REGISTRATION");

        //Variable to define the status of the registration
        $alreadyRegistered;
        //Variable can have the following status :
        //'n' = no registration
        //'r' = registered
        //'c' = cancelled
        //'w' = waiting

        if ($data_rowRegistration > 0){//If the registration exist   
            $DATARowRegistration = saprfc_table_read ($fce_get_registration,"TAB_REGISTRATION",1);
            //Set the status and id
            $alreadyRegistered = $DATARowRegistration['STATUS'];
        }
        else //No registration
            $alreadyRegistered = 'n';

        //realease the function
        saprfc_function_free($fce_get_registration);
        
        return $alreadyRegistered;
    }
    
    //Function to get the postrequis and set the status
    function get_postrequis($rfc, $array_themes_status, $id_prerequis){
        //-- FUNCTION MODULE: Z_FM_M646_THEME_POSTREQUIS --
        //We must know if the function really exists
        $fce_theme_postrequis = saprfc_function_discover($rfc, "Z_FM_M646_THEME_POSTREQUIS");
        if(!$fce_theme_postrequis){
            echo "The function module has failed.";
            echo $rfc;
            exit;
        }

        //Pass import parameters
        saprfc_import ($fce_theme_postrequis,"IV_THEMEID",$id_prerequis);

        //Pass table parameters
        saprfc_table_init ($fce_theme_postrequis,"TAB_POSTREQUIS");

        //Call and execute the function
        $rc_theme_postrequis = saprfc_call_and_receive ($fce_theme_postrequis);            
        if ($rc_theme_postrequis != SAPRFC_OK){
            if ($rfc == SAPRFC_EXCEPTION )
                echo ("Exception raised: ".saprfc_exception($fce_theme_postrequis));
            else
                echo ("Call error: ".saprfc_error($fce_theme_postrequis));
            exit;
        }

        //Fetch the data from the internal tables
        $data_row_theme_postrequis = saprfc_table_rows ($fce_theme_postrequis,"TAB_POSTREQUIS");

        if ($data_row_theme_postrequis > 0){//If there is/are postrequisites

            //Foreach postrequisites
            for ($y=1; $y<=$data_row_theme_postrequis; $y++){
                $DATARow_theme_postrequis = saprfc_table_read ($fce_theme_postrequis,"TAB_POSTREQUIS",$y);

                $current_postrequis = $DATARow_theme_postrequis['THEME_ID'];

                //Explore the array to set the status of the current_postrequis
                for($b = 0; $b < sizeof($array_themes_status); ++$b){
                    if($array_themes_status[$b][0] == $current_postrequis)
                        $array_themes_status[$b][1] = 'w';
                }
            }
        }
        //realease the function
        saprfc_function_free($fce_theme_postrequis); 
        
        return $array_themes_status;
    }
    
    function display_theme($texts, $DATARow){
        //Variables to display the date in a correct format
        $dateStart = substr($DATARow['STARTDATE'],-2).'.'.substr($DATARow['STARTDATE'],4,2).'.'.substr($DATARow['STARTDATE'],0,4);
        $dateEnd = substr($DATARow['ENDDATE'],-2).'.'.substr($DATARow['ENDDATE'],4,2).'.'.substr($DATARow['ENDDATE'],0,4);

        echo '<h3>'.$texts['theme'][$_SESSION['lang']].' '.htmlentities($DATARow['TITLE_THEME']).'</h3>';
        //Display dates (one or more days)
        echo '<p style="margin-left:10px;">';
        if($dateStart != $dateEnd )
            echo $texts['from'][$_SESSION['lang']] .' '. $dateStart .' '. $texts['to'][$_SESSION['lang']] .' '. $dateEnd;
        else
            echo $dateStart ;
        echo '<br/><br/>';
        echo htmlentities($DATARow['ADDRESS']).'<br/>';
        //Information
        echo $DATARow['ZIPCODE'].' '.htmlentities($DATARow['CITY']).'<br/>';
        //Language
        echo $texts['language'][$_SESSION['lang']].' : '.$DATARow['ID_LANGUAGE'];
    }
    
    echo '<div data-role="content">';
    
    //Check that the "theme" or "themes" parameter exist in the url
    if(isset ($_GET['theme'])) {

        //SAP connection
        $rfc = connectSAP();
        
        //To keep initial value of status
        $_SESSION['array_themes_status_initial'] = array();
        
        //Array with all countries
        $countries = array(
		"Afghanistan",
		"Albania",
		"Algeria",
		"Andorra",
		"Angola",
		"Antigua and Barbuda",
		"Argentina",
		"Armenia",
		"Australia",
		"Austria",
		"Azerbaijan",
		"Bahamas",
		"Bahrain",
		"Bangladesh",
		"Barbados",
		"Belarus",
		"Belgium",
		"Belize",
		"Benin",
		"Bhutan",
		"Bolivia",
		"Bosnia and Herzegovina",
		"Botswana",
		"Brazil",
		"Brunei",
		"Bulgaria",
		"Burkina Faso",
		"Burundi",
		"Cambodia",
		"Cameroon",
		"Canada",
		"Cape Verde",
		"Central African Republic",
		"Chad",
		"Chile",
		"China",
		"Colombi",
		"Comoros",
		"Congo (Brazzaville)",
		"Congo",
		"Costa Rica",
		"Cote d'Ivoire",
		"Croatia",
		"Cuba",
		"Cyprus",
		"Czech Republic",
		"Denmark",
		"Djibouti",
		"Dominica",
		"Dominican Republic",
		"East Timor (Timor Timur)",
		"Ecuador",
		"Egypt",
		"El Salvador",
		"Equatorial Guinea",
		"Eritrea",
		"Estonia",
		"Ethiopia",
		"Fiji",
		"Finland",
		"France",
		"Gabon",
		"Gambia, The",
		"Georgia",
		"Germany",
		"Ghana",
		"Greece",
		"Grenada",
		"Guatemala",
		"Guinea",
		"Guinea-Bissau",
		"Guyana",
		"Haiti",
		"Honduras",
		"Hungary",
		"Iceland",
		"India",
		"Indonesia",
		"Iran",
		"Iraq",
		"Ireland",
		"Israel",
		"Italy",
		"Jamaica",
		"Japan",
		"Jordan",
		"Kazakhstan",
		"Kenya",
		"Kiribati",
		"Korea, North",
		"Korea, South",
		"Kuwait",
		"Kyrgyzstan",
		"Laos",
		"Latvia",
		"Lebanon",
		"Lesotho",
		"Liberia",
		"Libya",
		"Liechtenstein",
		"Lithuania",
		"Luxembourg",
		"Macedonia",
		"Madagascar",
		"Malawi",
		"Malaysia",
		"Maldives",
		"Mali",
		"Malta",
		"Marshall Islands",
		"Mauritania",
		"Mauritius",
		"Mexico",
		"Micronesia",
		"Moldova",
		"Monaco",
		"Mongolia",
		"Morocco",
		"Mozambique",
		"Myanmar",
		"Namibia",
		"Nauru",
		"Nepa",
		"Netherlands",
		"New Zealand",
		"Nicaragua",
		"Niger",
		"Nigeria",
		"Norway",
		"Oman",
		"Pakistan",
		"Palau",
		"Panama",
		"Papua New Guinea",
		"Paraguay",
		"Peru",
		"Philippines",
		"Poland",
		"Portugal",
		"Qatar",
		"Romania",
		"Russia",
		"Rwanda",
		"Saint Kitts and Nevis",
		"Saint Lucia",
		"Saint Vincent",
		"Samoa",
		"San Marino",
		"Sao Tome and Principe",
		"Saudi Arabia",
		"Senegal",
		"Serbia and Montenegro",
		"Seychelles",
		"Sierra Leone",
		"Singapore",
		"Slovakia",
		"Slovenia",
		"Solomon Islands",
		"Somalia",
		"South Africa",
		"Spain",
		"Sri Lanka",
		"Sudan",
		"Suriname",
		"Swaziland",
		"Sweden",
		"Switzerland",
		"Syria",
		"Taiwan",
		"Tajikistan",
		"Tanzania",
		"Thailand",
		"Togo",
		"Tonga",
		"Trinidad and Tobago",
		"Tunisia",
		"Turkey",
		"Turkmenistan",
		"Tuvalu",
		"Uganda",
		"Ukraine",
		"United Arab Emirates",
		"United Kingdom",
		"United States",
		"Uruguay",
		"Uzbekistan",
		"Vanuatu",
		"Vatican City",
		"Venezuela",
		"Vietnam",
		"Yemen",
		"Zambia",
		"Zimbabwe"
	);
        
        if ($_SESSION['name'] == ''){//Guest
            if(isset($_POST['mail'])){
                //Test the if the email addresse is valid
                $atom   = '[-a-z0-9!#$%&\'*+\\/=?^_`{|}~]';
                $domain = '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)';

                $regex = '/^' . $atom . '+' .
                '(\.' . $atom . '+)*' .

                '@' .
                '(' . $domain . '{1,63}\.)+' .

                $domain . '{2,63}$/i';

                if (preg_match($regex, $_POST['mail']))
                   $mailValid = true;
                else
                   $mailValid = false;
                
                if(($_POST['mail']!='')&&($_POST['lastname']!='')&&($_POST['firstname']!='')
                &&($_POST['address']!='')&&($_POST['city']!='')&&($_POST['npa']!='')
                &&($_POST['country']!='')&&($mailValid == true)){
                    
                    $lastname = htmlentities($_POST['lastname'], ENT_QUOTES, 'UTF-8') ;
                    $firstname = htmlentities($_POST['firstname'], ENT_QUOTES, 'UTF-8') ;
                    $address = htmlentities($_POST['address'], ENT_QUOTES, 'UTF-8') ;
                    $city = htmlentities($_POST['city'], ENT_QUOTES, 'UTF-8') ;
                    $npa = htmlentities($_POST['npa'], ENT_QUOTES, 'UTF-8') ;
                    $country = htmlentities($_POST['country'], ENT_QUOTES, 'UTF-8') ;
                    $phone = htmlentities($_POST['phone'], ENT_QUOTES, 'UTF-8') ;
                    $mobile = htmlentities($_POST['mobile'], ENT_QUOTES, 'UTF-8') ;
                    $fax = htmlentities($_POST['fax'], ENT_QUOTES, 'UTF-8') ;
                    $mail = htmlentities($_POST['mail'], ENT_QUOTES, 'UTF-8') ;

                    $username = strtoupper($mail);

                    $_SESSION['login_guest'] = $username;

                    //Create user

                    //-- FUNCTION MODULE: Z_FM_M646_SET_PERSON_LDAP --
                    //We must know if the function really exists
                    $fce_personLDAP_new = saprfc_function_discover($rfc, "Z_FM_M646_SET_PERSON_LDAP");
                    if(!$fce_personLDAP_new){
                        echo "The function module has failed.";
                        echo $rfc;
                        exit;
                    }

                    //Pass import parameters
                    saprfc_import ($fce_personLDAP_new,"IV_MEMBER", 
                                    array ("USERNAME"=>strtoupper($username),
                                        "LASTNAME"=>$lastname, 
                                        "FIRSTNAME"=>$firstname, 
                                        "ADDRESS"=>$address,
                                        "ZIP_CODE"=>$npa,
                                        "CITY"=>$city, 
                                        "EMAIL"=>$mail, 
                                        "PHONE"=>$phone,
                                        "MOBILE_PHONE"=>$mobile,
                                        "COUNTRY"=>strtoupper($country),
                                        "FAX"=>$fax,  
                                        "INSTITUTION"=>'Invité'));

                    //Call and execute the function
                    $rc_registration_new = saprfc_call_and_receive ($fce_personLDAP_new);            
                    if ($rc_registration_new != SAPRFC_OK){
                        if ($rfc == SAPRFC_EXCEPTION )
                            echo ("Exception raised: ".saprfc_exception($fce_personLDAP_new));
                        else
                            echo ("Call error: ".saprfc_error($fce_personLDAP_new));
                        exit;
                    }

                    //realease the function
                    saprfc_function_free($fce_personLDAP_new);
                }
                else{//Formular
                    echo'<form method=\'POST\' action="./register.php?theme='.$_GET['theme'].'">';
                        if (($mailValid == true))
                            echo '<p align="center"><font color=#FF0000><b>'.$texts['fill'][$_SESSION['lang']].'</b></font></p>';
                        else
                           echo '<p align="center"><font color=#FF0000><b>'.$texts['invalid_email'][$_SESSION['lang']].'</b></font></p>';
                         
                        echo $texts['name'][$_SESSION['lang']] ."<font color=#FF0000><b>*</b></font> : <input type='text' name='lastname' id='lastname' value='".$_POST['lastname']."' maxlength='50' />";
                        echo $texts['firstname'][$_SESSION['lang']] ."<font color=#FF0000><b>*</b></font> : <input type='text' name='firstname' id='firstname' value='".$_POST['firstname']."'  maxlength='50' />";
                        echo $texts['address'][$_SESSION['lang']] ."<font color=#FF0000><b>*</b></font> : <input type='text' name='address' id='address' value='".$_POST['address']."'  maxlength='50' />";
                        echo $texts['npa'][$_SESSION['lang']] ."<font color=#FF0000><b>*</b></font> : <input type='text' name='npa' id='npa' value='".$_POST['npa']."'  maxlength='6' />";
                        echo $texts['city'][$_SESSION['lang']] ."<font color=#FF0000><b>*</b></font> : <input type='text' name='city' id='city' value='".$_POST['city']."'  maxlength='25' />";
                        echo $texts['country'][$_SESSION['lang']] ." : <select name=\"country\" id=\"country\">";
                        foreach ($countries as $key => $val){
                            echo "<option value=\"$val\">$val</option>";
                        }
                        echo '</select>';
                        echo $texts['phone'][$_SESSION['lang']] ." : <input type='text' name='phone' id='phone' value='".$_POST['phone']."'  maxlength='20' />";
                        echo $texts['mobile'][$_SESSION['lang']] ." : <input type='text' name='mobile' id='mobile' value='".$_POST['mobile']."'  maxlength='20' />";
                        echo $texts['fax'][$_SESSION['lang']] ." : <input type='text' name='fax' id='fax' value='".$_POST['fax']."'  maxlength='20' />";
                        echo $texts['email'][$_SESSION['lang']] ."<font color=#FF0000><b>*</b></font> : <input type='text' name='mail' id='mail' value='".$_POST['mail']."'  maxlength='50' />";
                        echo "<input type='submit' value='". $texts['validate'][$_SESSION['lang']] ."'/>";
                    echo '</form><p align="center">';
                }
            }
            else{//Formular
                echo'<form method=\'POST\' action="./register.php?theme='.$_GET['theme'].'">';
                    echo $texts['name'][$_SESSION['lang']] ." : <input type='text' name='lastname' id='lastname'  maxlength='50' />";
                    echo $texts['firstname'][$_SESSION['lang']] ." : <input type='text' name='firstname' id='firstname'  maxlength='50' />";
                    echo $texts['address'][$_SESSION['lang']] ." : <input type='text' name='address' id='address'  maxlength='50' />";
                    echo $texts['npa'][$_SESSION['lang']] ." : <input type='text' name='npa' id='npa'  maxlength='6' />";
                    echo $texts['city'][$_SESSION['lang']] ." : <input type='text' name='city' id='city'  maxlength='25' />";
                    echo $texts['country'][$_SESSION['lang']] ." : <select name=\"country\" id=\"country\">";
                        foreach ($countries as $key => $val){
                            echo "<option value=\"$val\">$val</option>";
                        }
                    echo '</select>';
                    echo $texts['phone'][$_SESSION['lang']] ." : <input type='text' name='phone' id='phone'  maxlength='20' />";
                    echo $texts['mobile'][$_SESSION['lang']] ." : <input type='text' name='mobile' id='mobile'  maxlength='20' />";
                    echo $texts['fax'][$_SESSION['lang']] ." : <input type='text' name='fax' id='fax'  maxlength='20' />";
                    echo $texts['email'][$_SESSION['lang']] ." : <input type='text' name='mail' id='mail'  maxlength='50' />";
                    echo "<input type='submit' value='". $texts['validate'][$_SESSION['lang']] ."'/>";
                echo '</form><p align="center">';
            }
            
            if(strstr($previous_page, 'detailEvent.php') != false){
                
                $array_themes_status_initial = array();
                $array_themes_status_initial = $_SESSION['array_themes_status'] ;
                
                for($i = 0; $i < sizeof($array_themes_status_initial); ++$i){
                    $array_themes_status_initial[$i][1] = get_registration($rfc, $array_themes_status_initial[$i][0], $username);
                }
                
                $_SESSION['array_themes_status_initial'] = $array_themes_status_initial;
                
                $array_themes_status = array();
                $array_themes_status = $array_themes_status_initial;
            }
        }
        else //Member
            $username = strtoupper($_SESSION['login']);
        
        if($username != ''){
        
            //Current event id
            $id_event;

            //Check that the "event" parameter exist in the url
            if(isset ($_GET['event']))
                $id_event = $_GET['event'];

            //Array to stock all themes
            $themes_array = array();

            if(strpos($_GET['theme'],'-')==false) //Only one theme
                $themes_array[0] = $_GET['theme'];
            else //More than one theme
                $themes_array = explode("-", $_GET['theme']);

            $_SESSION['array_themes_status_will'] = array();

            //Title
            echo '<h2>'.$texts['registration_to_theme'][$_SESSION['lang']].'</h2>';
            echo '<div class="tableauPage">';

            //Foreach themes
            for($i = 0; $i < sizeof($themes_array); ++$i){
                //Id of the current theme
                $id_theme = $themes_array[$i];    

                $DATARow = get_detail_theme($rfc, $id_theme);
               
                $nb_registration = get_nbParticipants($rfc, $id_theme);

                $status;

                //Test the number of people to know the status
                if ($nb_registration < $DATARow['MAXPEOPLE'])//Registration (Status: Registered)
                    $status = 'r';
                else//In waiting list (Status: Waiting)
                    $status = 'w';
                
                $previous_status = get_registration($rfc, $id_theme, $username);
                
                //Current theme id
                $id_event = htmlentities($DATARow['EVENT_ID']);
                
                display_theme($texts, $DATARow);
                
                //Check the prerequisites if the previous page was detailTheme.php
                if(strstr($previous_page, 'detailTheme.php') != false){
                    
                    //-- FUNCTION MODULE: Z_FM_M646_THEME_PREREQUIS --
                    //We must know if the function really exists
                    $fce_theme_prerequis = saprfc_function_discover($rfc, "Z_FM_M646_THEME_PREREQUIS");
                    if(!$fce_theme_prerequis){
                        echo "The function module has failed.";
                        echo $rfc;
                        exit;
                    }
                    
                    //Pass import parameters
                    saprfc_import ($fce_theme_prerequis,"IP_THEMEID",$id_theme);

                    //Pass table parameters
                    saprfc_table_init ($fce_theme_prerequis,"TAB_PREREQUIS");

                    //Call and execute the function
                    $rc_theme_prerequis = saprfc_call_and_receive ($fce_theme_prerequis);            
                    if ($rc_theme_prerequis != SAPRFC_OK){
                        if ($rfc == SAPRFC_EXCEPTION )
                            echo ("Exception raised: ".saprfc_exception($fce_theme_prerequis));
                        else
                            echo ("Call error: ".saprfc_error($fce_theme_prerequis));
                        exit;
                    }

                    //Fetch the data from the internal tables
                    $data_row_theme_prerequis = saprfc_table_rows ($fce_theme_prerequis,"TAB_PREREQUIS");
                    

                    if ($data_row_theme_prerequis > 0){//If there is/are prerequisites
                        
                        //Variable to stock all prerequis and status like (1_r-2_w)
                        $all_prerequis_status = '';
                        
                        //Foreach prerequisites
                        for ($i=1; $i<=$data_row_theme_prerequis; $i++){
                            $DATARow_theme_prerequis = saprfc_table_read ($fce_theme_prerequis,"TAB_PREREQUIS",$i);

                            $current_prerequis = $DATARow_theme_prerequis['THEME_ID'];

                            //Check if a registration already exist

                            //Variable to define the status of the registration
                            $alreadyRegistered = get_registration($rfc, $current_prerequis, $username);
                            //Variable can have the following status :
                            //'n' = no registration
                            //'r' = registered
                            //'c' = cancelled
                            //'w' = waiting

                            //Put all prerequis and the status
                            $all_prerequis_status.= $current_prerequis.'_'.$alreadyRegistered.'-';
                        }
                        
                        $all_prerequis_status = substr($all_prerequis_status, 0, strlen($all_prerequis_status)-1);
                         
                        //2 dimensions array with the theme and the prerequis (and the status)
                        $array_themes_status = array();
                        $array_themes_status[0][0] = $id_theme;
                        $array_themes_status[0][1] = $status;
                        
                        //Put all prerequis in the $array_themes_status
                        $array_prerequis = explode("-", $all_prerequis_status);
                        
                        //Variable to know if there will have prerequisite to register
                        $display_have_to_register = false;
                        
                        for($a = 0; $a < sizeof($array_prerequis); ++$a){
                            $position = strpos($array_prerequis[$a], '_');
                            $array_themes_status[$a+1][0] = substr($array_prerequis[$a], 0, $position);
                            $array_themes_status[$a+1][1] = substr($array_prerequis[$a], $position+1, strlen($array_prerequis[$a])-1);
                            if($array_themes_status[$a+1][1] == 'n' || $array_themes_status[$a+1][1] == 'c'){
                                $display_have_to_register = true;
                            }
                        }
                        
                        //To keep the inital values
                        $array_themes_status_initial = $array_themes_status;
                                               
                        //Foreach prerequis (only prerequis, not the current theme !)
                        for($a = 1; $a < sizeof($array_themes_status); ++$a){
                            
                            //Test the status
                            if($array_themes_status[$a][1] == 'n' || $array_themes_status[$a][1] == 'c'){//Not registration or cancelled
                               
                                $DATARow_prerequis = get_detail_theme($rfc, $array_themes_status[$a][0]);
                                
                                //Check the actual number registrations for these prerequis
                                $nb_registration_prerequis = get_nbParticipants($rfc, $array_themes_status[$a][0]);

                                $status_current_prerequis;

                                //Test the number of people to know the status
                                if ($nb_registration_prerequis < $DATARow_prerequis['MAXPEOPLE'])//Registration (Status: Registered)
                                    $status_current_prerequis = 'r';
                                else//In waiting list (Status: Waiting)
                                    $status_current_prerequis = 'w';

                                $array_themes_status[$a][1] = $status_current_prerequis;

                                //If in the waiting list, change all "postrequis" to be in the waiting list too
                                if($status_current_prerequis == 'w'){
                                    $array_themes_status = get_postrequis($rfc, $array_themes_status, $array_themes_status[$a][0]);                                                
                                }
                            }
                            else if($array_themes_status[$a][1] == 'w'){//In the waiting list
                                $array_themes_status = get_postrequis($rfc, $array_themes_status, $array_themes_status[$a][0]);
                            }
                        }
                        
                        for($a = 0; $a < sizeof($array_themes_status); ++$a){
                            if($array_themes_status[$a][1] == 'n' || $array_themes_status[$a][1] == 'c')
                                $array_themes_status[$a][1] = 'r';
                        }
                        
                        $array_themes_status_initial[0][1] = $previous_status;
                        
                        $_SESSION['array_themes_status_initial'] = $array_themes_status_initial;
                        
                        $_SESSION['array_themes_status_will'] = $array_themes_status;
                        
                        if($array_themes_status[0][1] == 'w')
                            echo '<br/><br/><b>'.$texts['will_waiting'][$_SESSION['lang']].'</b><br/></p>';

                        if ($display_have_to_register == true)
                            echo '<p><b>'.$texts['have_to_register'][$_SESSION['lang']].'</b></p>';
                        
                        for($a = 1; $a < sizeof($array_themes_status); ++$a){
                            if($array_themes_status_initial[$a][1] == n || $array_themes_status_initial[$a][1] == 'c'){
                                
                                $DATARow_display = get_detail_theme($rfc, $array_themes_status[$a][0]);
                                
                                display_theme($texts, $DATARow_display);

                                if($array_themes_status[$a][1] == 'w'){
                                    echo '<br/><br/><b>'.$texts['will_waiting'][$_SESSION['lang']].'</b>';
                                }
                                echo '<br/></p>';
                            }
                        }
                    }
                    else{//No prerequis
                        
                        $array_themes_status = array();
                        $array_themes_status[0][0] = $id_theme;
                        $array_themes_status[0][1] = $status;
                        
                        $array_themes_status_initial = array();
                        $array_themes_status_initial = $array_themes_status;
                        $array_themes_status_initial[0][1] = get_registration($rfc, $id_theme, $username);
                        
                        $_SESSION['array_themes_status_initial'] = $array_themes_status_initial;
                        $_SESSION['array_themes_status_will'] = $array_themes_status;
                        
                        if ($status == 'w')
                            echo '<br/><br/><b>'.$texts['will_waiting'][$_SESSION['lang']].'</b><br/></p>';
                    }
                    //realease the function
                    saprfc_function_free($fce_theme_prerequis);
                }
                //Check the prerequisites if the previous page was detailEvent.pgp
                else if(strstr($previous_page, 'detailEvent.php') != false){
                    
                    //Set empty variables
                    if($array_themes_status_initial == ''){
                    
                        $array_themes_status_initial = array();
                        
                        if(strpos($username,'@')==false){//Member
                            $array_themes_status_initial = $_SESSION['array_themes_status'] ;
                            $_SESSION['array_themes_status_initial'] = $_SESSION['array_themes_status'];
                        }

                        $array_themes_status = array();
                        $array_themes_status = $_SESSION['array_themes_status'];
                    }
                    
                    //First theme
                    if($i == 0){
                        for($j = 0; $j < sizeof($array_themes_status_initial); ++$j){
                            //For all subscribed theme in waiting list, put postrequis in waiting list to
                            if($array_themes_status_initial[$j][1] == 'w')
                                $array_themes_status = get_postrequis($rfc, $array_themes_status, $array_themes_status_initial[$j][0]);
                        }
                    }
            
                    for($j = 0; $j < sizeof($array_themes_status); ++$j){
                        if($array_themes_status[$j][0] == $id_theme){
                            if($status == 'r'){
                                if($array_themes_status[$j][1] != 'w')
                                    $array_themes_status[$j][1] = 'r';
                            }
                            else if($status == 'w'){
                                $array_themes_status[$j][1] = 'w';
                                $array_themes_status = get_postrequis($rfc, $array_themes_status, $id_theme);
                            }
                        }
                    }
                    
                    for($j = 0; $j < sizeof($array_themes_status); ++$j){
                        if($array_themes_status[$j][0] == $id_theme){
                            if ($array_themes_status[$j][1] == 'w')
                                echo '<br/><br/><b>'.$texts['will_waiting'][$_SESSION['lang']].'</b>';   
                        }
                    }
                    
                    //Last theme
                    if($i == sizeof($themes_array)-1){
                        for($y = 0; $y < sizeof($array_themes_status); ++$y){
                            $set_status = true;
                            for($z = 0; $z < sizeof($themes_array); ++$z){
                                if($array_themes_status[$y][0] == $themes_array[$z])
                                    $set_status = false;
                            }
                            if($set_status == true)
                                $array_themes_status[$y][1] = 'n';
                        }
      
                        $_SESSION['array_themes_status_will'] = $array_themes_status ;
                        echo '<br/></p>';
                    }
                }
            }
            
            //Display validate button
            echo '<p><a href="./registerValidate.php" data-role="button" data-inline="true">'. $texts['validate'][$_SESSION['lang']] .'</a>';
            //Display cancel button
            echo '<a href="'.$previous_page.'" data-role="button" data-inline="true">'. $texts['cancel'][$_SESSION['lang']] .'</a></p>';
            echo '</div>';
        }
        //close the connection
        saprfc_close($rfc);
    }
    else//Paramater "theme" doesn't exist
        display_theme_not_found($texts);

    echo '</div>';
    include('include/footer.inc.php');  // Call footer 
?>